package com.example.fresh_market.adapter;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat;
import android.support.v7.recyclerview.extensions.ListAdapter;
import android.support.v7.util.DiffUtil;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.request.RequestOptions;
import com.example.fresh_market.R;
import com.example.fresh_market.request.UsrProductPageResp;
import com.example.fresh_market.utils.HttpUtil;

import org.apache.commons.lang3.StringUtils;

public class ProductAdapter extends ListAdapter<UsrProductPageResp, ProductAdapter.ViewHolder> {

    public static DiffUtil.ItemCallback<UsrProductPageResp> DIFF_CALLBACK = new DiffUtil.ItemCallback<UsrProductPageResp>() {
        @Override
        public boolean areItemsTheSame(@NonNull UsrProductPageResp oldItem, @NonNull UsrProductPageResp newItem) {
            return oldItem.getId().equals(newItem.getId());
        }

        @Override
        public boolean areContentsTheSame(@NonNull UsrProductPageResp oldItem, @NonNull UsrProductPageResp newItem) {
            return oldItem.equals(newItem);
        }
    };
    private final Context mContext;
    private final MyClickListener mListener;

    public ProductAdapter(Context context, MyClickListener mListener) {
        super(DIFF_CALLBACK);
        this.mContext = context;
        this.mListener = mListener;
    }

    protected ProductAdapter(@NonNull DiffUtil.ItemCallback<UsrProductPageResp> diffCallback,
                             MyClickListener mListener, Context context) {
        super(diffCallback);
        this.mContext = context;
        this.mListener = mListener;
    }

    public UsrProductPageResp getItem(int position) {
        return super.getItem(position);
    }

    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
        //绑定xml组件
        return new ViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.prod_list_item, parent, false));
    }

    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        UsrProductPageResp product = getItem(position);
        holder.bindBean(product);
        if (StringUtils.isNotBlank(product.getImage())) {
            RequestOptions requestOptions = new RequestOptions()
                    .override(120, 160);
            HttpUtil.loadImage(holder.image, product.getImage(), requestOptions);
        } else {
            Drawable drawable = ContextCompat.getDrawable(mContext, R.drawable.logo);
            holder.image.setImageDrawable(drawable);
        }
        holder.chooseBt.setOnClickListener(v -> {
            mListener.onChooseClick(v, holder.getAdapterPosition());
        });
        holder.itemView.setOnClickListener(v -> {
            mListener.onItemClick(v, holder.getAdapterPosition());
        });
    }

    public interface MyClickListener {
        void onChooseClick(View v, int position);

        void onItemClick(View view, int position);
    }

    public static class ViewHolder extends RecyclerView.ViewHolder {
        private final TextView type;
        private final TextView productName;
        private final TextView productInfo;
        private final TextView productPrice;
        private final ImageView image;
        private final Button chooseBt;

        ViewHolder(View item) {
            super(item);
            type = item.findViewById(R.id.productType);
            productName = item.findViewById(R.id.productName);
            productInfo = item.findViewById(R.id.productInfo);
            productPrice = item.findViewById(R.id.productPrice);
            image = item.findViewById(R.id.productImage);
            chooseBt = item.findViewById(R.id.BT_choose);
        }

        void bindBean(final UsrProductPageResp product) {
            type.setText(product.getType());
            productName.setText(product.getName());
            productInfo.setText(product.getInfo());
            productPrice.setText(String.format("￥%s", product.getPrice()));
        }
    }
}
